<?
if (!defined('SMF'))
	die('Hacking attempt...');
	
global $level_factor, $torp_dmg_rate;

echo BigTitle($txt['bnt_sf_attacking']);

$targetfighters = $total_sector_fighters;
$playerbeams = NUM_BEAMS($playerinfo['beams']);

if ($calledfrom == 'rsmove')
	$playerinfo['ship_energy'] += $energyscooped;

if ($playerbeams > $playerinfo['ship_energy'])
	$playerbeams = $playerinfo['ship_energy'];

$playerinfo['ship_energy'] = $playerinfo['ship_energy'] - $playerbeams;
$playershields = NUM_SHIELDS($playerinfo['shields']);

if ($playershields > $playerinfo['ship_energy'])
	$playershields = $playerinfo['ship_energy'];

// $playerinfo[ship_energy]=$playerinfo[ship_energy]-$playershields;
$playertorpnum = round(mypw($level_factor,$playerinfo['torp_launchers'])) * 2;

if ($playertorpnum > $playerinfo['torps'])
	$playertorpnum = $playerinfo['torps'];

$playertorpdmg = $torp_dmg_rate * $playertorpnum;
$playerarmor = $playerinfo['armor_pts'];
$playerfighters = $playerinfo['ship_fighters'];

if ($targetfighters > 0 && $playerbeams > 0)
{
	if ($playerbeams > round($targetfighters / 2))
	{
		$temp = round($targetfighters / 2);
		$lost = $targetfighters - $temp;
		$txt['bnt_sf_destfight'] = str_replace("[lost]", $lost, $txt['bnt_sf_destfight']);
		echo $txt['bnt_sf_destfight'];
		$targetfighters = $temp;
		$playerbeams = $playerbeams - $lost;
	}
	else
	{
		$targetfighters = $targetfighters - $playerbeams;
		$txt['bnt_sf_destfightb'] = str_replace("[lost]", $playerbeams, $txt['bnt_sf_destfightb']);
		echo $txt['bnt_sf_destfightb'];
		$playerbeams = 0;
	}   
}

echo '<br>', $txt['bnt_sf_torphit'], '<br>';

if ($targetfighters > 0 && $playertorpdmg > 0)
{
	if ($playertorpdmg > round($targetfighters / 2))
	{
		$temp = round($targetfighters / 2);
		$lost = $targetfighters - $temp;
		$txt['bnt_sf_destfightt'] = str_replace("[lost]", $lost, $txt['bnt_sf_destfightt']);
		echo $txt['bnt_sf_destfightt'];
		$targetfighters = $temp;
		$playertorpdmg = $playertorpdmg - $lost;
	}
	else
	{
		$targetfighters = $targetfighters - $playertorpdmg;
		$txt['bnt_sf_destfightt'] = str_replace("[lost]", $playertorpdmg, $txt['bnt_sf_destfightt']);
		echo $txt['bnt_sf_destfightt'];
		$playertorpdmg = 0;
	}
}

echo '<br>', $txt['bnt_sf_fighthit'], '<br><br>';

if ($playerfighters > 0 && $targetfighters > 0)
{
	if ($playerfighters > $targetfighters)
	{
		echo $txt['bnt_sf_destfightall'];
		$temptargfighters=0;
	}
	else
	{
		$txt['bnt_sf_destfightt2'] = str_replace("[lost]", $playerfighters, $txt['bnt_sf_destfightt2']);
		echo $txt['bnt_sf_destfightt2'];
		$temptargfighters = $targetfighters - $playerfighters;
	}
	
	if ($targetfighters > $playerfighters)
	{
		echo $txt['bnt_sf_lostfight'];
		$tempplayfighters = 0;
	}
	else
	{
		$txt['bnt_sf_lostfight2'] = str_replace("[lost]", $targetfighters, $txt['bnt_sf_lostfight2']);
		echo $txt['bnt_sf_lostfight2'];
		$tempplayfighters = $playerfighters - $targetfighters;
	}     
	$playerfighters = $tempplayfighters;
	$targetfighters = $temptargfighters;
}

if ($targetfighters > 0)
{
	if ($targetfighters > $playerarmor)
	{
		$playerarmor = 0;
		echo $txt['bnt_sf_armorbreach'];
	}
	else
	{
		$playerarmor = $playerarmor - $targetfighters;
		$txt['bnt_sf_armorbreach2'] = str_replace("[lost]", $targetfighters, $txt['bnt_sf_armorbreach2']);
		echo $txt['bnt_sf_armorbreach2'];
	} 
}

$fighterslost = $total_sector_fighters - $targetfighters;
destroy_fighters($sector, $fighterslost);

$txt['bnt_sf_sendlog'] = str_replace("[player]", $playerinfo['character_name'], $txt['bnt_sf_sendlog']);
$txt['bnt_sf_sendlog'] = str_replace("[lost]", $fighterslost, $txt['bnt_sf_sendlog']);
$txt['bnt_sf_sendlog'] = str_replace("[sector]", $sector, $txt['bnt_sf_sendlog']);
message_defence_owner($sector, $txt['bnt_sf_sendlog']);

playerlog($playerinfo['ship_id'], LOG_DEFS_DESTROYED_F, "$fighterslost|$sector");

$armor_lost = $playerinfo['armor_pts'] - $playerarmor;
$fighters_lost = $playerinfo['ship_fighters'] - $playerfighters;
$energy = $playerinfo['ship_energy'];

$result = $smcFunc['db_query']('', '
	UPDATE {raw:db_ships}
	SET ship_energy = {int:energy}, ship_fighters = ship_fighters - {int:fighters_lost}, armor_pts = armor_pts - {int:armor_lost}, torps = torps - {int:playertorpnum}
	WHERE ship_id = {int:ship_id}',
	array(
		'db_ships' => $dbtables['ships'],
		'energy' => $energy,
		'fighters_lost' => $fighters_lost,
		'armor_lost' => $armor_lost,
		'playertorpnum' => $playertorpnum,
		'ship_id' => $playerinfo['ship_id']
	)
);

$txt['bnt_sf_lreport'] = str_replace("[armor]", $armor_lost, $txt['bnt_sf_lreport']);
$txt['bnt_sf_lreport']= str_replace("[fighters]", $fighters_lost, $txt['bnt_sf_lreport']);
$txt['bnt_sf_lreport']= str_replace("[torps]", $playertorpnum, $txt['bnt_sf_lreport']);
echo $txt['bnt_sf_lreport'];

if ($playerarmor < 1)
{
	echo $txt['bnt_sf_shipdestroyed'];
	
	playerlog($playerinfo['ship_id'], LOG_DEFS_KABOOM, "$sector|$playerinfo[dev_escapepod]");
	
	$txt['bnt_sf_sendlog2'] = str_replace("[player]", $playerinfo['character_name'], $txt['bnt_sf_sendlog2']);
	$txt['bnt_sf_sendlog2'] = str_replace("[sector]", $sector, $txt['bnt_sf_sendlog2']);
	message_defence_owner($sector,$txt['bnt_sf_sendlog2']);
	
	if ($playerinfo['dev_escapepod'] == "Y")
	{	
		global $start_energy;
		$rating = round($playerinfo['rating'] / 2);
		echo $txt['bnt_sf_escape'];
		
		$result = $smcFunc['db_query']('', '
			UPDATE {raw:db_ships}
			SET hull = 0,
				engines = 0,
				power = 0,
				sensors = 0,
				computer = 0,
				beams = 0,
				torp_launchers = 0,
				torps = 0,
				armor = 0,
				armor_pts = 100,
				cloak = 0,
				shields = 0,
				sector = 0,
				ship_organics = 0,
				ship_ore = 0,
				ship_goods = 0,
				ship_energy = {int:start_energy},
				ship_colonists = 0,
				ship_fighters = 100,
				dev_warpedit = 0,
				dev_genesis = 0,
				dev_beacon = 0,
				dev_emerwarp = 0,
				dev_escapepod = "N",
				dev_fuelscoop = "N",
				dev_minedeflector = 0,
				on_planet = "N",
				rating = {int:rating},
				cleared_defences = NULL,
				dev_lssd = "N"
			WHERE ship_id = {int:ship_id}',
			array(
				'db_ships' => $dbtables['ships'],
				'start_energy' => $start_energy,
				'rating' => $rating,
				'ship_id' => $playerinfo['ship_id']
			)
		);
		
		cancel_bounty($playerinfo['ship_id']);
		$ok = 0;
		TextMainMenu();
		return;
	}
	else
	{ 
		cancel_bounty($playerinfo['ship_id']);
		db_kill_player($playerinfo['ship_id']);
		$ok = 0;
		TextMainMenu();
		return;
	}         
}

if ($targetfighters > 0)
	$ok = 0;
else
	$ok = 2;
?>
